package com.loong.controller;

import cn.dsna.util.images.ValidateCode;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 生成验证码
 */
@WebServlet("/code")
public class CodeController extends BaseServlet{

    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 生成验证码
        ValidateCode validateCode = new ValidateCode(100,35,5,20);
        // 将验证码保存到session中
        String code = validateCode.getCode();
        request.getSession().setAttribute("code", code);
        //向页面写回验证码
        ServletOutputStream outputStream = response.getOutputStream();
        validateCode.write(outputStream);
    }
}
